﻿using DMS.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace DMS.Utils
{
    public static class FileRepository
    {
        public static String RootStorage
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + @"UploadFiles\";
            }
        }

        public static String[] GetAllDocuments()
        {
            String[] all_files = Directory.GetFiles(RootStorage,"*.*", System.IO.SearchOption.AllDirectories);
            String[] output = all_files.Select(Path.GetFileName).Where(one_file => one_file != "vs.bin").ToArray();
            return output;
        }

        public static Boolean IsPresent(String Filename)
        {
            return GetAllDocuments().Contains(Filename);
        }
    }
    public class Utils
    {
        /// <summary>
        /// Lay description cua enum
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetDescriptionFromEnumValue<T>(object enumValue)
        {
            if (Enum.IsDefined(typeof(T), enumValue))
            {
                var value = (T)enumValue;
                DescriptionAttribute attribute = value.GetType()
                    .GetField(value.ToString())
                    .GetCustomAttributes(typeof(DescriptionAttribute), false)
                    .SingleOrDefault() as DescriptionAttribute;
                return attribute == null ? value.ToString() : attribute.Description;
            }
            else
            {
                return string.Empty;
            }
        }

        public static string MD5Hash(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();
        }

        public static string CreateSalt(string UserName)
        {
            string tempSalt = UserName;
            if (UserName.Length < 8)
            {
                tempSalt = UserName + "manutd";
            }
            Rfc2898DeriveBytes hasher = new Rfc2898DeriveBytes(UserName,
                System.Text.Encoding.Default.GetBytes(tempSalt), 10000);
            return Convert.ToBase64String(hasher.GetBytes(25));
        }

        public static string HashPassword(string Salt, string Password)
        {
            Rfc2898DeriveBytes Hasher = new Rfc2898DeriveBytes(Password,
                System.Text.Encoding.Default.GetBytes(Salt), 10000);
            return Convert.ToBase64String(Hasher.GetBytes(25));
        }

        public static string FlashFormatView = "<embed width=\"100%\" height=\"109\" allowfullscreen=\"true\" wmode=\"Transparent\" quality=\"high\" name=\"rotator\" src=\"{0}\"  type=\"application/x-shockwave-flash\">";
        public static string FlashImageView = "<img src='{0}' style='width:150px'/>";

        
    }

    public class ConstKey
    {
        public static string SignInToken = "SigninToken";
    }

    public class Enums
    {
        //0: PDF; 1: DOC, DOCX, 2: PPT..
        public enum DocumentType
        {
            [Description("PDF")]
            PDF = 0,
            [Description("DOC")]
            DOC = 1,
            [Description("PPT")]
            PPT = 2
        }

        //Vi tri banner: 0: header; 1: right; 2: bottom; 3: left
        public enum BannerPosition
        {
            [Description("Header")]
            Header = 0,
            [Description("Right")]
            Right = 1,
            [Description("Bottom")]
            Bottom = 2,
            [Description("Left")]
            Left = 3
        }

        //Loai banner: 0: Image; 1: Flash; 2: Html
        public enum BannerType
        {
            [Description("Image")]
            Image = 0,
            [Description("Flash")]
            Flash = 1,
            [Description("Html")]
            Html = 2
        }
    }
}